#include "Common.hpp"
#include "Protocol.hpp"

void Usage(const std::string& proc){
    std::cerr << proc << " server_ip" << " server_port" << std::endl;
}

void GetDataFromStdin(int* x, int* y, char* oper){
    std::cout << "Please Enter X: ";
    std::cin >> *x;
    std::cout << "Please Enter Y: ";
    std::cin >> *y;
    std::cout << "Please Enter Oper: ";
    std::cin >> *oper;
}

int main(int argc, char* argv[]){
    if(argc != 3){
        Usage(argv[0]);
        exit(USAGE_ERR);
    }

    std::string server_ip = argv[1];
    uint16_t server_port = std::stoi(argv[2]);

    std::shared_ptr<Socket> sock = std::make_shared<TcpSocket>();
    sock->BuildTcpClientSocketMethod();
    if(sock->Connect(server_ip, server_port) != 0){
        std::cerr << "Connect error" << std::endl;
        exit(CONNECT_ERR);
    }


    std::unique_ptr<Protocol> protocol = std::make_unique<Protocol>();
    std::string resp_buff;
    while(true){
        int x, y;
        char oper;
        GetDataFromStdin(&x, &y, &oper);
        std::string req_buffer = protocol->BuildRequestString(x, y, oper);
        sock->Send(req_buffer);

        Response resp;
        bool res = protocol->GetResponse(sock, resp_buff, &resp);
        if(res == false)
            break;
        resp.ShowResult();
    }

    return 0;
}